package com.citms.controller;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/test")
public class TestUploadController {
	
	@RequestMapping("/fileUpload")
	public void fileUpload(@RequestParam("files") MultipartFile[] file,HttpServletResponse res,HttpServletRequest req) throws IOException {
		
		Enumeration<String> headerNames = req.getHeaderNames();
		for (;headerNames.hasMoreElements();) {
			String nextElement = headerNames.nextElement();
			System.out.println(nextElement+":"+req.getHeader(nextElement));
		}
		System.out.println(req.getParameterMap());

		if (null != file && file.length > 0) {
			// 遍历并保存文件
			for (MultipartFile files : file) {
				if (file != null) {
					// 取得当前上传文件的文件名称
					String fileName = files.getOriginalFilename();
					System.out.println(fileName);
					res.getWriter().write(fileName);
				}
			}
		} else {
			//System.out.println("error");
			res.getWriter().write("error");
		}
	}
}
